/*
 * @(#)WarningAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;

/**
  WarningAction.java

  This class is for maint update alert action

    @author          Jenny Li
    @company         DCIVision Limited
    @creation date   23/05/2005
    @version         $Revision: 1.7.2.1 $
*/

public class DmsFilterDocTypeAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.7.2.1 $";

  protected String sListName = null;

  public DmsFilterDocTypeAction() {
    super();
        }

  /**
  * getMajorDAOClassName
  *
  * @return  The class name of the major DAObject will be used in this action.
  */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
  }

  /**
  * getFunctionCode
  *
  * @return  The corresponding system function code of action.
  */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PERSONAL_FOLDER);
  }
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
        // Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    DmsFilterDocTypeForm filterForm=(DmsFilterDocTypeForm)form;
    String opMode = (filterForm!=null) ? filterForm.getOpMode() : request.getParameter("opMode");    //maintForm.getOpMode();
    String displayDocType=filterForm.getDisplayDocType();
    if(Utility.isEmpty(filterForm.getDocumentType())){
    if(!Utility.isEmpty(displayDocType)){
      String[] documentType=new String[displayDocType.length()];
      int i=0;
      if(displayDocType.indexOf(DmsDocument.DOCUMENT_TYPE)>=0){
        documentType[i]=DmsDocument.DOCUMENT_TYPE;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.FOLDER_TYPE)>=0){
        documentType[i]=DmsDocument.FOLDER_TYPE;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.DOCUMENT_LINK)>=0){
        documentType[i]=DmsDocument.DOCUMENT_LINK;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.COMPOUND_DOC_TYPE)>=0){
        documentType[i]=DmsDocument.COMPOUND_DOC_TYPE;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.PAPER_DOC_TYPE)>=0){
        documentType[i]=DmsDocument.PAPER_DOC_TYPE;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.EMAIL_DOC_TYPE)>=0){
        documentType[i]=DmsDocument.EMAIL_DOC_TYPE;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.FORM_DOC_TYPE)>=0){
        documentType[i]=DmsDocument.FORM_DOC_TYPE;
        i++;
      }
      if(displayDocType.indexOf(DmsDocument.FLOW_DOC_TYPE)>=0){
        documentType[i]=DmsDocument.FLOW_DOC_TYPE;
        i++;
      }
      filterForm.setDocumentType(documentType);
    }
    }else{
      displayDocType="";
      for(int i=0;i<filterForm.getDocumentType().length;i++){
        displayDocType=displayDocType+filterForm.getDocumentType()[i];
      }
      filterForm.setDisplayDocType(displayDocType);
    }
    if (GlobalConstant.OP_MODE_INSERT.equals(opMode)) {
      try {
        this.insertRecord(mapping, filterForm, request, response);
        this.commit(request);
        addMessage(request, MESSAGE_RECORD_INSERT);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
        filterForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
      } else {
        filterForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
        return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
      }
    }else{
      return super.execute(mapping,form,request,response);
    }
  }
  
  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    DmsFilterDocTypeForm filterForm=(DmsFilterDocTypeForm)form;
    String[] documentType=filterForm.getDocumentType();
    String displayDocType="";
    if(!Utility.isEmpty(documentType)){         
      for(int i=0;i<documentType.length;i++){
        displayDocType=displayDocType+documentType[i];
      }
    }
    request.getSession().setAttribute(DmsOperationConstant.DISPLAY_DOC_TYPE,displayDocType);
    request.getSession().setAttribute("createUser",this.getSessionContainer(request).getUserRecordID());
  }
  
}
